unit oTurniquet;

interface

uses oLogicalEntity, XMLIntf, oEntityTypeConstants, oIstoreable, oGeneratorData, uOSPRand;

type
  TTurniquet = class(TLogicalEntity)
    strict private
      aGeneratorData: TGeneratorData;
      aGenerator: tRndm;
      procedure setGeneratorData(const Value: TGeneratorData);
      function defaultGenerator: TGeneratorData;
    public
      property GeneratorData: TGeneratorData read aGeneratorData write setGeneratorData;
      property Generator: tRndm read aGenerator;
      destructor Destroy; override;
      procedure save(paKam: IXMLNode;paKtory: Integer);
      function vypis:String;
      procedure SaveToXML(var paNode : Ixmlnode); override;
      procedure LoadFromXML(paNode : IXMLNode); override;
      constructor Create(paName : string; paId : integer; paGeneratorData: TGeneratorData = nil); overload;
      constructor Create; overload;
  end;

implementation

uses SysUtils, oMasterManager, oProcessObjects, oProcessItem, oProcessObjectConstants;

{ TTurniket }

constructor TTurniquet.Create;
begin
  inherited Create;
  TypeLogic := ET_TURNIQUET_TYPE;
  aGeneratorData := nil;
  aGenerator := nil;
  GeneratorData := defaultGenerator;
end;

function TTurniquet.defaultGenerator: TGeneratorData;
begin
  result := TExponentialGeneratorData.create(genExponential,1,0,10,true);
end;

constructor TTurniquet.Create(paName: string; paId: integer; paGeneratorData: TGeneratorData = nil);
begin
  Create;
  inherited Create(paName, paId);
  //nutne cez property
  if paGeneratorData <> nil
    then GeneratorData := paGeneratorData
    //defaultny generator
    else GeneratorData := defaultGenerator;
end;

destructor TTurniquet.destroy;
begin
  aGeneratorData.Free;
  aGenerator.Free;
  inherited;
end;

procedure TTurniquet.LoadFromXML(paNode: IXMLNode);
var
  data: TProcessingGeneratorData;
  ArrTypeAttribute: IXMLNode;
  item: TProcessItem;
begin
  inherited LoadFromXML(paNode.ChildNodes['log_entity']);
  ArrTypeAttribute := paNode.ChildNodes.FindNode('Generator');
  if ArrTypeAttribute = nil
    then begin
      GeneratorData := defaultGenerator;
    end else begin
      item := tMasterManager.EntityProcessObject(TProcessItem.create(LOAD_GENERATOR,TProcessingXML.Create(ArrTypeAttribute)));
      data := item.Data as TProcessingGeneratorData;
      GeneratorData := data.Data;
    end;
end;

procedure TTurniquet.save(paKam: IXMLNode; paKtory: Integer);
var
  temp: IXMLNode;
begin
  temp := paKam.AddChild('turniket_' + inttostr(paKtory));
end;

procedure TTurniquet.SaveToXML(var paNode: Ixmlnode);
var
  n : IXMLNode;
  ArrTypeAttribute: IXMLNode;
begin
  n := paNode.AddChild('turniquet');
  inherited SaveToXML(n);
  ArrTypeAttribute := n.AddChild('Generator');
  tMasterManager.EntityProcessObject(TProcessItem.create(SAVE_GENERATOR,TXMLGenerator.Create(ArrTypeAttribute,aGeneratorData)));
end;

procedure TTurniquet.setGeneratorData(const Value: TGeneratorData);
var
  item: TProcessItem;
begin
  if Value = nil then exit;
  aGenerator.Free;
//  aGeneratorData.Free;
  item := tMasterManager.EntityProcessObject(TProcessItem.create(CONVERT_DATA_TO_GENERATOR,TProcessingGeneratorData.create(Value)));
  aGenerator := (item.Data as TprocessingGenerator).Generator;
  aGeneratorData := Value;
  item.Free;
end;

function TTurniquet.vypis: String;
begin
//  result := 'X:' + inttostr(PosX) + ' Y: ' + inttostr(PosY);
end;

end.
